package com.vanda.db;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vanda.db.cfg.Configuration;
import com.vanda.db.cfg.Environment;
import com.vanda.db.exception.CreateException;
import com.vanda.util.LogFunc;

/**
 * 取得数据库接口工厂类. <br>
 * 
 * @author 王瑛<br>
 * @version Version 1.00<br>
 */
public class DBFactory {
	/** 取得日志类 */
	private static Log log = LogFactory.getLog(DBFactory.class);
	/** HQL标志 */
	static final int ACCESSOR_TYPE_HQL = 0;
	/** SQL标志 */
	static final int ACCESSOR_TYPE_SQL = 1;
	static {
		try {
			//读取配置文件
			new Configuration().configure();
		} catch (Exception e) {
			LogFunc.info("Configuration Error : " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 用默认default数据源生成HQLAccessor实例，
	 * @return
	 * @throws CreateException
	 */
	public static HQLAccessor getHQLAccessor() throws CreateException {
		return (HQLAccessor) getAccessor(
			Environment.HIBERNATE_DEFAULT,
			ACCESSOR_TYPE_HQL);
	}
	/**
	 * 根据指定的connectionName生成HQLAccessor实例
	 * @param connectionName 数据源名称
	 * @return
	 * @throws CreateException
	 */
	public static HQLAccessor getHQLAccessor(String connectionName)
		throws CreateException {
		return (HQLAccessor) getAccessor(connectionName, ACCESSOR_TYPE_HQL);
	}
	/**
	 * 用默认default数据源生成SQLAccessor实例，
	 * @return
	 * @throws CreateException
	 */
	public static SQLAccessor getSQLAccessor() throws CreateException {
		return (SQLAccessor) getAccessor(
			Environment.HIBERNATE_DEFAULT,
			ACCESSOR_TYPE_SQL);
	}
	/**
	 * 根据指定的connectionName生成SQLAccessor实例
	 * @param connectionName 数据源名称
	 * @return
	 * @throws CreateException
	 */
	public static SQLAccessor getSQLAccessor(String connectionName)
		throws CreateException {
		return (SQLAccessor) getAccessor(connectionName, ACCESSOR_TYPE_SQL);
	}
	/**
	 * 生成XMLAccessor实例
	 * @return
	 */
	public static XMLAccessor getXMLAccessor() {
		return new XMLAccessor();
	}
	
	/**
	 * 根据指定的connectionName和accessorType生成对应的Accessor实例
	 * @param connectionName 数据源名称
	 * @param accessorType accessor类型
	 * @return
	 * @throws CreateException
	 */
	static Accessor getAccessor(String connectionName, int accessorType)
		throws CreateException {
		Accessor accessor = null;
		Session session = null;
		try {
			session = HibernateUtil.currentSession(connectionName);
			accessor = getAccessor(session, accessorType);
		} catch (HibernateException e) {
			throw new CreateException("Create Accessor Falied!", e);
		} catch (CreateException e) {
			throw e;
		}
		return accessor;
	}
	
	/**
	 * 根据Session和accessorType生成对应的Accessor实例
	 * @param session Hibernate Session
	 * @param accessorType accessor类型
	 * @return
	 * @throws CreateException
	 */
	static Accessor getAccessor(Session session, int accessorType)
		throws CreateException {
		Accessor accessor = null;
		try {
			if (accessorType == ACCESSOR_TYPE_HQL) {
				accessor = new HQLAccessor(session);
			} else if (accessorType == ACCESSOR_TYPE_SQL) {
				accessor = new SQLAccessor(session);
			} else {
				log.error("accessorType value error!");
				throw new CreateException("accessorType value error!");
			}
		} catch (Exception e) {
			try {
				if (session != null) {
					session.close();
				}
			} catch (Exception ee) {
				ee.printStackTrace();
			}
			throw new CreateException("Create Accessor Falied!", e);
		}
		return accessor;
	}
}
